/*
----------------------------------------------------------------------------
Copyright (c) 2005 - 2008, Wangxing SHI	<swxlion@gmail.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.

* Neither the copyright holder's name nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------
*/

/*----------------------------- TSimpleMap.h ------------------------------*/
/*===========================================================================
FILE: TSimpleMap.h
Author: Wangxing SHI (swxlion)
Version: 0.0.2 
Date: 2008-03-29

File Description:
    C++ template for a simple map.

Dependencies:
    Platform: Platform Independent.

NOTE:
	The implement can't be used with Memory Pool.
History:
version        author        date       description
----------------------------------------------------
0.0.1          swxlion       07-08-20   Created the first version. 
0.0.2          swxlion       08-03-29   Integrated in the ASDL namespace.
===========================================================================*/
#ifndef _APPLICATION_SERVER_DEVELOPMENT_LIBRARY__TSIMPLE_MAP_H_
#define _APPLICATION_SERVER_DEVELOPMENT_LIBRARY__TSIMPLE_MAP_H_

/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "TRBTree.h"
#include <new>
/*===============================================================================
CLASS & STRUCTURE DEFINITIONS
=============================================================================== */
namespace ASDL
{
	template<typename First, typename Second >
	class TSimpleMap
	{
	public:
		class Pair
		{
		public:
			First		first;
			Second		second;

			Pair(): first(), second()
			{
			}
			Pair(const First &f, const Second &s): first(f), second(s)
			{
			}

			//-- Overload the operators of "<", ">", "<=", ">=", and "==".
			//-- Operator "=" using the default assignation operator.
			bool operator == ( const Pair &r ) const
			{
				return (first == r.first) ? true : false;
			}
			bool operator > ( const Pair &r ) const
			{
				return (first > r.first) ? true : false;
			}
			bool operator < ( const Pair &r ) const
			{
				return (first < r.first) ? true : false;
			}
			bool operator != ( const Pair &r ) const
			{
				return (first != r.first) ? true : false;
			}
			bool operator <= ( const Pair &r ) const
			{
				return (first <= r.first) ? true : false;
			}
			bool operator >= ( const Pair &r ) const
			{
				return (first >= r.first) ? true : false;
			}
		};

		typedef Pair			*iterator;

	private:
		TRBTree< Pair >			clsTree;

	public:
		TSimpleMap(): clsTree()
		{
		}

		~TSimpleMap()
		{
			clear();
		}

		inline bool		insert( First f, Second s );
		inline void		erase( const First &f );	
		inline void		clear();

		inline iterator	begin()
		{
			typedef typename TRBTree< typename TSimpleMap<First, Second>::Pair >::RBNode		RBNODE, *LPRBNODE;

			LPRBNODE	lpFindNode = clsTree.FindMin();
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}
		inline iterator	end()
		{
			typedef typename TRBTree< typename TSimpleMap<First, Second>::Pair >::RBNode		RBNODE, *LPRBNODE;

			LPRBNODE	lpFindNode = clsTree.FindMax();
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}
		inline iterator	find( const First &f )
		{
			typedef typename TRBTree< typename TSimpleMap<First, Second>::Pair >::RBNode		RBNODE, *LPRBNODE;

			Pair		MapNode;

			MapNode.first = f;
			LPRBNODE lpFindNode = clsTree.Find( MapNode );
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}

		inline iterator FindLarger( iterator iter )
		{
			typedef typename TRBTree< typename TSimpleMap<First, Second>::Pair >::RBNode		RBNODE, *LPRBNODE;

			LPRBNODE lpFindNode = clsTree.FindLarger( *iter );
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}

		inline iterator FindSmaller( iterator iter )
		{
			typedef typename TRBTree< typename TSimpleMap<First, Second>::Pair >::RBNode		RBNODE, *LPRBNODE;

			LPRBNODE lpFindNode = clsTree.FindSmaller( *iter );
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}
	};

	/*===============================================================================
	INLINE FUNCTION DEFINITIONS: Simple Map Functions: Class TSimpleMap
	=============================================================================== */
	template<typename First, typename Second >
	bool TSimpleMap<First, Second>::insert( First f, Second s )
	{
		typedef typename TRBTree< typename TSimpleMap<First, Second>::Pair >::RBNode		RBNODE, *LPRBNODE;

		LPRBNODE	lpInsertNode = new(std::nothrow) RBNODE;
		if( !lpInsertNode )
			return false;

		lpInsertNode->Data.first  = f;
		lpInsertNode->Data.second = s;

		clsTree.Insert( lpInsertNode );
		return true;
	}

	template<typename First, typename Second >
	void TSimpleMap<First, Second>::erase( const First &f )
	{
		typedef typename TRBTree< typename TSimpleMap<First, Second>::Pair >::RBNode		RBNODE, *LPRBNODE;

		Pair		DeleteMapNode;

		DeleteMapNode.first = f;
		clsTree.Delete( DeleteMapNode );
		//LPRBNODE lpDeleteRBNode = clsTree.Find( DeleteMapNode );
		//clsTree.Remove( lpDeleteRBNode );
		//delete lpDeleteRBNode;
	}

	template<typename First, typename Second >
	void TSimpleMap<First, Second>::clear()
	{
		clsTree.Clear();
	}
}

#endif // _APPLICATION_SERVER_DEVELOPMENT_LIBRARY__TSIMPLE_MAP_H_
